import { CoolController, BaseController, CoolUrlTag, TagTypes } from '@cool-midway/core';
import { CircleService } from '../../service/circle';
import { Body, Get, Inject, Post, Query } from '@midwayjs/core';

/**
 * 圈子
 */
@CoolUrlTag({ key: TagTypes.IGNORE_TOKEN, value: ['getCircleData', 'getFineCircle', 'getCircleList', 'getCircleDetail'] })
@CoolController()
export class CircleController extends BaseController {
    @Inject()
    circleService: CircleService;

    /**
     * 圈子数据
     */
    @Get('/getCircleData')
    async getCircleData() {
        const { success, message, data } = await this.circleService.getCircleData();
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 精选圈子
     */
    @Get('/getFineCircle')
    async getFineCircle() {
        const { success, message, data } = await this.circleService.getFineCircle();
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 圈子列表
     */
    @Get('/getCircleList')
    async getCircleList() {
        const { success, message, data } = await this.circleService.getCircleList();
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 圈子详情
     * @param id 圈子id
     */
    @Get('/getCircleDetail')
    async getCircleDetail(@Query('id') id: number) {
        const { success, message, data } = await this.circleService.getCircleDetail(id);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 我的圈子
     */
    @Get('/getMyCircle')
    async getMyCircle() {
        const { success, message, data } = await this.circleService.getMyCircle();
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 加入圈子
     * @param id 圈子id
     */
    @Post('/joinCircle', { summary: '加入圈子' })
    async joinCircle(@Body() params) {
        const { id } = params;
        const { success, message, data } = await this.circleService.joinCircle(id);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 退出圈子
     * @param id 圈子id
     */
    @Post('/exitCircle', { summary: '退出圈子' })
    async exitCircle(@Body() params) {
        const { id } = params;
        const { success, message, data } = await this.circleService.exitCircle(id);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }

    /**
     * 创建圈子
     * @param name 圈子名称
     * @param description 圈子描述
     * @param images 圈子封面
     * @param categoryIds 圈子分类
     */
    @Post('/createCircle', { summary: '创建圈子' })
    async createCircle(@Body() params) {
        const { name, description, images, categoryIds } = params;
        const { success, message, data } = await this.circleService.createCircle(name, description, images, categoryIds);
        if (!success) return this.fail(data, message);
        return this.ok(data, message);
    }
}
